/**
 * Created by Richard Radlherr.
 * Date: 04.06.2006 19:54
 */

using System;

namespace TagTool.Core
{
	public class ValueChangedEventArgs : EventArgs
	{
		Object value;
		
		public ValueChangedEventArgs(Object value)
		{
			this.value = value;
		}
		
		public Object Value
		{
			get { return value; }
		}
		
	}
	
	public delegate void ValueChangedEventHandler(Object sender, ValueChangedEventArgs e);

	public class OutputSlot : Slot
	{
		protected Object value;
		public ValueChangedEventHandler ValueChanged = null;

		public OutputSlot(BaseNode owner, String name, Type argumentType) : base(owner, name, argumentType)
		{
		}
		
		public override bool Allowed(Object obj)
		{
			return obj.GetType() == ArgumentType || obj.GetType().IsSubclassOf(ArgumentType);
		}

		public override Object Value 
		{
			get 
			{
				return value; 
			}

			set {
				if (Allowed(value))
				{
					this.value = value;
					OnValueChanged();
				}
				else
				{
					throw new ArgumentException(String.Format("A value of type {0} is not allowed as value of {1}", value.GetType().ToString(), Name));
				}
			}
		}
		
		protected virtual void OnValueChanged()
		{
			if (ValueChanged != null)
			{
				ValueChanged(this, new ValueChangedEventArgs(value));
			}
		}

		public override bool Editable
		{
			get { return false; }
		}

		public override void Parse(String description)
		{
		}
	}
}
